﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LogicBaseLibrary.Zip
{
    public class FileItem
    {
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        string path;

        public string Path
        {
            get { return path; }
            set { path = value; }
        }

        long compressLength;

        public long CompressLength
        {
            get { return compressLength; }
            set { compressLength = value; }
        }

        long unCompressLength;

        public long UnCompressLength
        {
            get { return unCompressLength; }
            set { unCompressLength = value; }
        }

        int index;

        public int Index
        {
            get { return index; }
            set { index = value; }
        }
        public FileItem(string name, string path, long compressLength, long unCompressLength)
        {
            this.name = name;
            this.path = path;
            this.compressLength = compressLength;
            this.unCompressLength = unCompressLength;
            this.index = 0;
        }
        public override string ToString()
        {


            return string.Format("{0},{1},{2},{3},{4}", this.name, this.path, this.compressLength, this.unCompressLength, this.index);
        }
        public byte[] ToBytes()
        {
            List<byte> byteList = new List<byte>();
            byte[] nameByte = Encoding.UTF8.GetBytes(this.name);
            byte[] pathByte = Encoding.UTF8.GetBytes(this.path);
            byteList.AddRange(BitConverter.GetBytes(nameByte.Length));
            byteList.AddRange(nameByte);
            byteList.AddRange(BitConverter.GetBytes(pathByte.Length));
            byteList.AddRange(pathByte);
            byteList.AddRange(BitConverter.GetBytes(this.compressLength));
            byteList.AddRange(BitConverter.GetBytes(this.unCompressLength));
            
            return byteList.ToArray();
        }
        public static FileItem FromByte(byte[] buffer)
        {
            FileItem fi = null;
            string fileName = string.Empty;
            string path = string.Empty;
            long compressLength = 0;
            long unCompressLength = 0;
            int fieldcount = 0;
            int lastFieldindex = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                //if find then end of field
                if (buffer[i] == 26)
                {
                    fieldcount++;
                    byte[] fieldBuffer = new byte[i-lastFieldindex];
                    Array.Copy(buffer, lastFieldindex, fieldBuffer, 0, fieldBuffer.Length);
                    switch (fieldcount)
                    { 
                        case 1://filename
                            fileName = Encoding.UTF8.GetString(fieldBuffer);
                            break;
                        case 2://path
                            path = Encoding.UTF8.GetString(fieldBuffer);
                            break;
                        case 3://compressLength
                            compressLength = BitConverter.ToInt64(fieldBuffer,0);
                            break;
                        case 4://uncompressLength
                            unCompressLength = BitConverter.ToInt64(fieldBuffer, 0);
                            break;
                    }
                    lastFieldindex = i+1;
                }
            }
            fi = new FileItem(fileName, path, compressLength, unCompressLength);
            return fi;
        }
    }

}
